package start.demo01.fileIO;

import java.io.*;

public class Task05 {
    public static void main(String[] args) throws IOException {
        String path="D:/iotest3";
        String name="Stream.txt";
        File dir=new File(path);
        File file =new File(dir,name);
        dir.mkdir();
        file.createNewFile();
        try(BufferedWriter writer=new BufferedWriter(new FileWriter(file));
            BufferedReader reader=new BufferedReader(new FileReader(file));){
            String str="我叫王承建,性别:男,出生年月:19980913";
            /*
            * 将·覆盖原文件
            * */
            writer.write(str);
            writer.close();//需要关闭
            //int len=-1;
//            while((len=reader.read())!=-1){
//                System.out.print((char)len);
//            }
            String val=null;
            while ((val = reader.readLine()) != null) {
                //读取每一行，并输出
                System.out.print(val);
            }
        }
        copy(file,new File(dir,"test.txt"));
    }
    public static void copy(File f1,File f2) throws IOException {
        try(BufferedWriter writer=new BufferedWriter(new FileWriter(f2));
            BufferedReader reader=new BufferedReader(new FileReader(f1));){
            int len;
            while((len=reader.read())!=-1){
                writer.write((char)len);//关闭流后再打开，才会覆盖原文件
            }
        }
    }

}
